<template>
  <div class="update-name">
    <van-nav-bar
      @click-left="$emit('close')"
      @click-right="onClickRight"
      left-text="取消"
      right-text="完成"
      title="昵称"
    />
    <div class="field-wrap">
      <van-field
        autofocus
        autosize
        maxlength="7"
        placeholder="请输入昵称"
        rows="2"
        show-word-limit
        type="textarea"
        v-model.trim="localname"
      />
    </div>
  </div>
</template>

<script>

import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localname: this.value
    }
  },
  methods: {
    async onClickRight () {

      this.$toast.loading({
        message: '保存中',
        forbidClick: true,
        duration: 0
      })
      try {
        const localname = this.localname
        if (!localname.length) {
          this.$toast('昵称不能为空')
          return
        }
        const { data } = await updateUserProfile({
          name: localname
        })
        console.log(data);
        this.$emit('input', localname)
        this.$emit('close')

        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }

    }
  }
}
</script>
<style lang="less" scoped>
.field-wrap {
  padding: 20px;
}
</style>